/* 
* @Author: LDF QQ 47121862
* @Date:   2014-05-21 18:26:12
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-05-21 21:11:34
*/
//++++++++++倒计时+++++++++
//传入日期，格式为2014/05/01 
//opt参数可选
$.fn.extend({
	revTime:function(dates,opt){
		var html=null,timer=null,_this=this;
		this.getTime=function(d){
			var n= new Date(),
			 	 f = new Date(d),
			 	 re={},diff;
			diff = f.getTime()-n.getTime(); //两个日期的时间差
			var d = parseInt( diff/(1000*60*60*24) ); //获得天数
			diff = diff%(1000*60*60*24); //除去天
			var h = parseInt( diff/(1000*60*60) ); //获得小时数
			diff = diff%(1000*60*60); //除去小时
			var i = parseInt( diff/(1000*60) ); //获得分钟数
			diff = diff%(1000*60); //除去分钟
			var s = parseInt( diff/1000 ); //获得秒数
			return re = {d:d,h:h,i:i,s:s};
		}
		this.addHtml=function(){
			html='\
				<b class="Day"></b> 天 \
				<b class="Hour"></b> 小时 \
				<b class="Min"></b> 分钟 \
				<b class="Sec"></b> 秒\
			';
			_this.append(html);
			return this;
		}
		this._echo=function(){ //自定义参数
			var r = _this.getTime(dates);
			_this.children('.Day').html(r.d);
			_this.children('.Hour').html(r.h);
			_this.children('.Min').html(r.i);
			_this.children('.Sec').html(r.s);
			return this;
		}
		if(!opt)_this.addHtml()._echo();
		timer = setInterval(function(){
			_this._echo();
		},1000)
		return this;
	},
	drag:function(limit){ //拖拽,默认不限制范围
		var id = $(this);
		id.css('cursor','move');
		id.mousedown(function(e){
			var ls = e.pageX - $(this).position().left,
			 	 ts = e.pageY - $(this).position().top,
			 	 w = $(window).width() - $(this).width(),
			 	 h = $(window).height() - $(this).height() + $(window).scrollTop(),
			 	 _this=$(this);	
			$(document).on('mousemove',function(e){
				var l = e.pageX -ls,
			 	 	 t = e.pageY - ts;	
				_this.css({'left':l,'top':t});
				if(limit){
					if(_this.position().left<=0)
						_this.css('left',0);
					if(_this.position().top<=0)
						_this.css('top',0);
					if(_this.position().left>=w)
						_this.css('left',w);
					if(_this.position().top>=h)
						_this.css('top',h);
				}
			})
			return false;
		})
		$(document).on('mouseup',function(){
			$(document).off('mousemove');
		})
		return this;
	}
});